package com.zee.example

import android.os.Bundle
import com.zee.base.ZActivity
import com.zee.http.MyOk
import com.zee.http.request.DownloadFileCallBackListener
import com.zee.listener.OnPermissionListener
import com.zee.log.ZLog
import com.zee.utils.SuperZPerMissionUtils
import kotlinx.android.synthetic.main.activity_down_file.*
import java.io.File
import java.lang.Exception

class DownFileActivity : ZActivity() {
    override fun getLayoutResID(): Int {
        return R.layout.activity_down_file
    }

    override fun initViews(savedInstanceState: Bundle?) {
        val http = "http://c.shouji.360tpcdn.com/210113/e376eb76cd7d8b1755cd042d090e58aa/com.ss.android.article.news_8070.apk"
        tv_down.setOnClickListener {
            SuperZPerMissionUtils.getInstance().add("android.permission.WRITE_EXTERNAL_STORAGE").requestPermissions(object : OnPermissionListener {
                override fun onPerMission(deniedPermissions: MutableList<String>?, permissionExplain: MutableList<String>?) {
                    ZLog.i("===<<")
                }
            }, this)


            MyOk.downloadFile(http).tag("https").execute(object : DownloadFileCallBackListener() {
                override fun onError(e: Exception) {
                    ZLog.i("---" + e.message)
                }

                override fun onDownloadSuccess(file: File?) {
                    ZLog.i("----下载完成${file?.absolutePath}")
                }

                override fun onDownloadProgress(progress: Float, currentSize: Long, totalSize: Long, networkSpeed: Long) {
                    ZLog.i("$progress--->$currentSize--->当前大小$totalSize")
                    progressBar.setProgress(progress.toInt())
                }

            })
        }

        tv_stop.setOnClickListener {
            MyOk.getInstance().cancelTag("https")
        }
    }
}